/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package networkvisualize;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hrushi
 */
public class FileHandler {

    private String name;
    private int mode;

    private File file;
    private FileReader fileReader;
    private FileWriter fileWriter;
    private BufferedReader reader;
    private BufferedWriter writer;

    private static int WRITE=1;
    private static int READ=0;

    public FileHandler (String fileName) {
        name = fileName;
    }

    public String[] readIntoArray() {

        String[] strArr;
        String text = null;
        String str = null;
        StringBuffer contents = new StringBuffer();

        try {
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

        str=contents.toString();
        strArr = str.split("\\n");
        return strArr;
    }

    public String getSecondValueFromName(String name) {
        String[] strArr;
        String text = null;
        String str = null;
        StringBuffer contents = new StringBuffer();

        try {
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

        str=contents.toString();
        strArr = str.split("\\n");

        for (int i=0; i<strArr.length; i++) {
            String[] tempStr;
            tempStr = strArr[i].split(" ");
            if (tempStr[0].trim().equals(name.trim())) {
                return tempStr[1];
            }
        }

        return null;
    }

    public boolean openFile (int fileMode) {
        mode = fileMode;

            try {
                if (mode == READ) {
                    file = new File (name);
                    fileReader = new FileReader(file);
                    reader = new BufferedReader (fileReader);
                }
                else {
                    file = new File(name);
                    fileWriter = new FileWriter(file);
                    writer = new BufferedWriter(fileWriter);
                }
                return true;

            } catch (FileNotFoundException ex) {
                System.out.println("filenotfound exc openfile");
                return false;
                //ex.printStackTrace();
            } catch (IOException ex) {
                System.out.println("io  exc openfile");
                return false;
                //Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    public void closeFile () {
        try {
            if (mode == READ) {
                reader.close();
                fileReader.close();
                file = null;
            }
            else {
                writer.close();
                fileWriter.close();
                file = null;
            }

        } catch (IOException ex) {
            System.out.println("inside closefile");
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
